﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Watson.Core.Tools;
using System.Web.SessionState;
using ITOMMS.Entity;
using System.Collections.Generic;
using ITOMMS.Business;

namespace ITOMMS.Web.Ajax
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class NewMsgTip : IHttpHandler, IReadOnlySessionState, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //不让浏览器缓存 
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            string Type = context.Request.Params["Type"];
            string condtion = string.Empty;
            switch (Type)
            {
                case ("tip"):

                    try
                    {
                        context.Response.Write(GetNotDowithTrouble());
                    }
                    catch (Exception ex)
                    {
                        log4netHelper.error("智能提示查询出错", ex);
                        return;
                    }
                    break;
                case ("role"):

                    try
                    {
                        context.Response.Write(GetNeedTip());
                    }
                    catch (Exception ex)
                    {
                        log4netHelper.error("智能提示查询出错", ex);
                        return;
                    }
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        /// <summary>
        /// 根据权限确定那些人登陆要提醒。
        /// 客户不用
        /// </summary>
        /// <returns></returns>
        private string GetNeedTip()
        {
            if (SessionHelper.UserRoleGroup != PubEnums.RoleGroup.客户.ToString())
            {
                return "OK";
            }
            else
            {
               return "";
            }
        }

        /// <summary>
        /// 到数据库查询要提醒的内容
        /// </summary>
        /// <returns></returns>
        private string GetNotDowithTrouble()
        {
            string rs = string.Empty;
            Entity.tbContractEntity centity = new ITOMMS.Entity.tbContractEntity();
            List<tbContractEntity> ContractList = centity.GetAllByQuery("ExpirationTime<='" + System.DateTime.Now.AddDays(3).ToString("yyyy-MM-dd") + "'");
            if (ContractList.Count > 0)
            {
                rs += "您有合同即将到期，请处理！<br/>";
            }


            Entity.tbTroubleFormEntity entity = new ITOMMS.Entity.tbTroubleFormEntity();
            List<tbTroubleFormEntity> troubleList = entity.GetAllByQuery("State=" + Business.PubEnums.TroubleState.未处理.ToString("D"));
            if (troubleList.Count > 0)
            {
                rs += "您有新的故障单，请查收！<br/>";
            }

            Entity.tbWorkPlanEntity workPlanEntity = new ITOMMS.Entity.tbWorkPlanEntity();
            List<tbWorkPlanEntity> workPlanList = workPlanEntity.GetAllByQuery("State=" + Business.PubEnums.TroubleState.未处理.ToString("D") + " and DoDate<='" + System.DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + "'");
            if (workPlanList.Count > 0)
            {
                rs += "您有待办事项需要处理！";
            }

            Entity.tbContractEntity contractEntity = new ITOMMS.Entity.tbContractEntity();
            Entity.tbContractEntity contractListMain = contractEntity.FindEntity("ExpirationTime>='" + DateTime.Now.ToString("yyyy-MM-dd") + "'");
            string sSQL = "";
            tbContractEntity Centity = new tbContractEntity();
            List<tbContractEntity> contractListSub = null;

            DataSet ds = new DataSet();
            ds = contractEntity.GetDataSet("select * from tb_Contract where  ExpirationTime>='" + DateTime.Now.ToString("yyyy-MM-dd") + "'");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (contractListMain.提醒类别.ToString() == PubEnums.ConstractRemindType.月度.ToString("D"))
                {
                    sSQL = "ExpirationTime>='" + DateTime.Now.ToString("yyyy-MM-dd") + "'  and day(提醒日期)-1='" + System.DateTime.Now.AddDays(-1).Day.ToString() + "'";
                }
                else if (contractListMain.提醒类别.ToString() == PubEnums.ConstractRemindType.季度.ToString("D"))
                {
                    sSQL = "ExpirationTime>='" + DateTime.Now.ToString("yyyy-MM-dd") + "'  and CONVERT(varchar(100), 提醒日期, 23)='" + System.DateTime.Now.ToString("yyyy-MM-dd") + "'";

                    contractListSub = contractEntity.GetAllByQuery(sSQL);
                    if (contractListSub.Count > 0)
                    {
                        Centity = new tbContractEntity();
                        Centity = Centity.GetById(int.Parse(dr["id"].ToString()));
                        Centity.提醒日期 = DateTime.Parse(System.DateTime.Now.AddMonths(3).ToString("yyyy-MM-dd"));
                        Centity.SaveOrUpdate();
                    }
                }
                else if (contractListMain.提醒类别.ToString() == PubEnums.ConstractRemindType.半年.ToString("D"))
                {
                    sSQL = "ExpirationTime>='" + DateTime.Now.ToString("yyyy-MM-dd") + "'  and CONVERT(varchar(100), 提醒日期, 23)='" + System.DateTime.Now.ToString("yyyy-MM-dd") + "'";

                    contractListSub = contractEntity.GetAllByQuery(sSQL);
                    if (contractListSub.Count > 0)
                    {
                        Centity = new tbContractEntity();
                        Centity = Centity.GetById(int.Parse(dr["id"].ToString()));
                        Centity.提醒日期 = DateTime.Parse(System.DateTime.Now.AddMonths(6).ToString("yyyy-MM-dd"));
                        Centity.SaveOrUpdate();
                    }
                }
                else
                {
                    sSQL = "ExpirationTime>='" + DateTime.Now.ToString("yyyy-MM-dd") + "'  and CONVERT(varchar(100), 提醒日期, 23)='" + System.DateTime.Now.ToString("yyyy-MM-dd") + "'";

                    contractListSub = contractEntity.GetAllByQuery(sSQL);
                    if (contractListSub.Count > 0)
                    {
                        Centity = new tbContractEntity();
                        Centity = Centity.GetById(int.Parse(dr["id"].ToString()));
                        Centity.提醒日期 = DateTime.Parse(System.DateTime.Now.AddYears(1).ToString("yyyy-MM-dd"));
                        Centity.SaveOrUpdate();
                    }
                }
            }


            contractListSub = contractEntity.GetAllByQuery(sSQL);
            if (contractListSub.Count > 0)
            {
                rs += "您有合同缴费日期已到！";
            }

            return rs;
        }


        //private string GetExpirationTimeContract()
        //{
        //    string rs = string.Empty;
        //    Entity.tbContractEntity entity = new ITOMMS.Entity.tbContractEntity();
        //    List<tbContractEntity> ContractList = entity.GetAllByQuery("ExpirationTime>=" + System.DateTime.Now.ToString("yyyy-MM-dd"));
        //    if (ContractList.Count > 0)
        //    {
        //        rs = "有合同已经过期！";
        //    }


        //    return rs;
        //}



    }
}
